<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function form_input($data = '', $value = '', $extra = '') {
    $val = !is_array($data)?cek_value($data):cek_value($data['name']);
    if(!empty($val)) {
        $value=$val;
    }
    $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
    
    return prefix_open($data)."<input "._parse_form_attributes($data, $defaults).$extra." />".prefix_close();
}

function form_textarea($data = '', $value = '', $extra = '') {
    $val = !is_array($data)?cek_value($data):cek_value($data['name']);
    if(!empty($val)) {
        $value=$val;
    }
    $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');

    if ( ! is_array($data) OR ! isset($data['value'])) {
        $val = $value;
    }
    else {
        $val = $data['value'];
        unset($data['value']); // textareas don't use the value attribute
    }

    $name = (is_array($data)) ? $data['name'] : $data;
    return  prefix_open($data)."<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>".prefix_close();
}

function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '',$data=array()) {
    $data['name'] = $name;
    $val = !is_array($selected)?cek_value($name):$selected;
    if(!empty($val)) {
        $selected=$val;
    }

    if ( ! is_array($selected)) {

        $selected = array($selected);
    }
    
    // If no selected state was submitted we will attempt to set it automatically
    if (count($selected) === 0) {
        // If the form name appears in the $_POST array we have a winner!
        if (isset($_POST[$name])) {
            $selected = array($_POST[$name]);
        }
    }

    if ($extra != '') $extra = ' '.$extra;

    $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';

    $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";

    foreach ($options as $key => $val) {
        $key = (string) $key;

        if (is_array($val)) {
            $form .= '<optgroup label="'.$key.'">'."\n";

            foreach ($val as $optgroup_key => $optgroup_val) {
                $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';

                $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
            }

            $form .= '</optgroup>'."\n";
        }
        else {
            $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';

            $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
        }
    }

    $form .= '</select>';

    return  prefix_open($data).$form.prefix_close();
}

function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '') {
    $val = !is_array($data)?cek_value($data):cek_value($data['name']);
    if(!empty($val)) {
        $value=$val;
    }
    $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);

    if (is_array($data) AND array_key_exists('checked', $data)) {
        $checked = $data['checked'];

        if ($checked == FALSE) {
            unset($data['checked']);
        }
        else {
            $data['checked'] = 'checked';
        }
    }

    if ($checked == TRUE) {
        $defaults['checked'] = 'checked';
    }
    else {
        unset($defaults['checked']);
    }

    return  prefix_open($data)."<input "._parse_form_attributes($data, $defaults).$extra." />".prefix_close();
}

function prefix_open($data="") {
    
    $label = "";
    $subject = "";
    if(is_array($data)) {
        if(isset($data['label']))
            $subject = $data['label'];
        else {
            $subject = str_replace("_", " ", $data['name']);
            $subject = ucwords($subject);
        }
    }elseif(!empty($data)) {
        $subject = str_replace("_", " ", $data);
        $subject = ucwords($subject);
    }
    $class_label = isset($data['class_label'])?$data['class_label']:"subject";
    $class_div = isset($data['class_label'])?$data['class_label']:"input";
    $label = "<label class='$class_label'>$subject</label>";
    $prefix = "<div class='$class_div'>";
    return $prefix.$label;
}

function prefix_close() {
    return "</div>";
}

function cek_value($field="") {
    $nama = $field;
    $nama = str_replace("[", ".", $nama);
    $nama = str_replace("]", "", $nama);
    $nama = str_replace("..", ".", $nama);
    $arr = explode(".",$nama);
    $nama = implode("']['",$arr);
    $nama = "['".$nama."']";
    $nilai = "";

    eval("\$ada=isset(\$_POST".$nama.");");
    if($ada) {
        eval("\$nilai=\$_POST".$nama.";");
    }
    return $nilai;

}